package cn.utils;

import com.baidu.aip.util.Base64Util;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class FileUtils {

   public static Map<String, Object> uploadtest(MultipartFile file, String openid) {
       Map<String, Object> map = new HashMap<>();
       String imgName = openid + file.getOriginalFilename();
       try {
           // 包含图片的字节数组
           byte[] bytes = file.getBytes();
           // 将图片字节数组进行Base64编码
           String encode = cn.utils.imgs.Base64Util.encode(bytes);

           String encode1 = URLEncoder.encode(encode, StandardCharsets.UTF_8);

           // 输入文件到置顶路径
           FileUtils.fileOutput(BaseUtils.getProString("imgSavePath") + imgName, bytes);

           map.put("cloudPath", BaseUtils.getProString("imgSaveUrl") + "/images/" + imgName);
           map.put("localPath", encode1);
       } catch (IOException e) {
           e.printStackTrace();
       }
       return map;
   }

    /*public static Map<String, Object> uploadFile(MultipartFile file, String openid) {
        // 定义

        String imgUrl = "";

        String imgName = openid + file.getOriginalFilename();

        String localPath = BaseUtils.getProperties("imgSavePath") + imgName;
        String cloudPath = BaseUtils.getProperties("imgSaveUrl") + imgName;

        File img = new File(localPath);

        byte[] imgByte = new byte[(int) file.getSize()];
        Map<String, Object> res = new HashMap<>();
        try(InputStream imgIs = new FileInputStream(img)) {
            // 将文件写入指定路径
            file.transferTo(img);
            // 获取写入文件的输入流

            // 将文件读取到byte字节数组
            while (imgIs.read(imgByte) != -1) {}


            // 将字节数组进行base64编码
            imgUrl = Base64Util.encode(imgByte);

            imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8);

            res.put("localPath", imgUrl);
            res.put("cloudPath", cloudPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }*/

    /**
     * 指定输出路径，跟包含文件数据的字节数组写文件
     * @param path 文件输出路径
     * @param file 包含问渐渐数据的字节数组
     * @return 是否写入成功
     */
    public static boolean fileOutput(String path, byte[] file) {
        try (OutputStream os = new BufferedOutputStream(new FileOutputStream(new File(path)))) {
            os.write(file);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static boolean fileOutput(File file, byte[] files) {
        try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
            os.write(files);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
